package me.zhengjie.modules.system.service.impl;

import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.system.domain.UserInsuranceInfo;
import me.zhengjie.modules.system.repository.UserInsuranceInfoRepository;
import me.zhengjie.modules.system.service.UserInsuranceInfoService;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
@RequiredArgsConstructor
@CacheConfig(cacheNames = "userInsuranceInfo")
public class UserInsuranceInfoServiceImpl implements UserInsuranceInfoService {

    private final UserInsuranceInfoRepository userInsuranceInfoRepository;

    @Override
    public void addInfo(UserInsuranceInfo userInsuranceInfo) {
        UserInsuranceInfo save = userInsuranceInfoRepository.save(userInsuranceInfo);
        System.out.println(save);
    }

    @Override
    public List<UserInsuranceInfo> queryList(String insuranceCode, String acceptance) {
        if (StringUtils.isEmpty(acceptance)) {
            return userInsuranceInfoRepository.queryList(insuranceCode);
        }
        return userInsuranceInfoRepository.queryList(insuranceCode, acceptance);
    }

    @Override
    public void delete(Long id) {
        userInsuranceInfoRepository.deleteById(id);
    }

    @Override
    public UserInsuranceInfo findById(Long id) {
        return userInsuranceInfoRepository.queryById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void acceptance(Long id) {
        userInsuranceInfoRepository.acceptance(id);
    }
}
